//index.js
//获取应用实例

const app = getApp();
let Api = require('../../api/api.js');

Page({
    data: {
        search:'',
        swiperData: [],
        indicatorDots: false,
        autoplay: false,
        duration: 1000,
        vertical:true,
        pageindex:1,
        showCloseBtn:false,
        current:0,
        tabbarIndex:0,
        showModal:false,
        modalImg:'',
        wpPrice: '',
        wpPrice_1: '',
        nameplate:'请选择产品铭牌',
        nameplateListData: [],
        activeNameplateList:0,
        purchaseQuantity:1,
        sendType:'',
        wpName:'',
        activeWpCover:'',
        wsid:'',
    },
    onLoad: function () {
        let self = this;
        let tokenValue = wx.getStorageSync('token');
        app.globalData.token = tokenValue;
        console.log('----------tokenValue----------------', tokenValue)
        console.log('----------app.globalData.token----------------', app.globalData.token)
        if (tokenValue === undefined || tokenValue === "" || tokenValue === null) {
            wx.login({
                success: res => {
                    wx.setStorageSync('code', res.code)
                    //重新授权
                    wx.redirectTo({
                        url: '/pages/auth/auth?loginType=index',
                    })
                }
            })
        }
        //2）token身份检测是否存在 检测token有没有失效
        Api.get(app.globalData.loginDomain, {
            rtype: 'checktoken',
            token: app.globalData.token,
        }).then(res => {
            console.log(res, ' code=0则正确，code别的值测错误（则需要重新获取用户身份')
            //code=0则正确，code别的值测错误（则需要重新获取用户身份）
            if (res.data.code != "0") {
                wx.login({
                    success: res => {
                        wx.setStorageSync('code', res.code)
                        //重新授权
                        wx.redirectTo({
                            url: '/pages/auth/auth?loginType=index',
                        })
                    }
                })
            }else{
                Api.get(app.globalData.mallDomain, {//获取所有商品（或 取单个商品）接口
                    doAllProduct: 1,
                    pagesize: 10,
                    pageindex: 1,
                    token: app.globalData.token,
                }).then(res => {
                    console.log(res)
                    let list = [];
                    if (res.data.list != '') {
                        res.data.list.forEach(ele => {
                            list.push({
                                wpID: ele.wpID,
                                wpName: ele.wpName,
                                wpViceName: ele.wpViceName,
                                wpPrice: parseInt(ele.wpPrice),
                                wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                                wpOldPrice: parseInt(ele.wpOldPrice),
                                wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                                wpCover: ele.wpCover,
                                wpImgs: ele.wpImgs,
                                wpPeos: ele.wpPeos,
                                wpOrigin: ele.wpOrigin,
                                wpDetail: ele.wpDetail,
                                SiteDomainName: ele.SiteDomainName,
                            })
                        })
                    }
                    self.setData({
                        swiperData: list,
                        modalImg: list[0].wpCover,
                        wpPrice_1: list[0].wpPrice_1,
                        wpPrice: list[0].wpPrice,
                    })

                })
            }
        })
        
    },

    /**
     * 进入确认订单页面
     */
    toOrderPage: function () {
        let self = this;
        self.setData({
            showModal: false,
        })
        wx.navigateTo({
            url: '../confirmOrder/confirmOrder?sendType=' + self.data.sendType + "&nameplateType=" + self.data.nameplate + '&purchaseQuantity=' + self.data.purchaseQuantity + '&wpid=' + app.globalData.wpid + '&enterType=首页进入&wpName=' + self.data.wpName + '&wpPrice=' + self.data.wpPrice + '.' + self.data.wpPrice_1 + '&wpCover=' + self.data.activeWpCover + '&wsid=' + self.data.wsid 
        })
    },
    /**
     * 送自己，送他人
     */
    clickSend: function (e) {
        let self = this;
        self.setData({
            showModal: true,
            wpName: e.currentTarget.dataset.item.wpName,
            activeWpCover: e.currentTarget.dataset.item.wpCover,
        })
        app.globalData.wpid = e.currentTarget.dataset.wpid; //全局储存商品属性标识ID 

        Api.get(app.globalData.mallDomain, { //2）获取商品属性
            doGetProductAttribute: 1,
            wpid: e.currentTarget.dataset.wpid,
        }).then(res => {
            if (res.data.onevalue != ''){
                self.setData({
                    nameplateListData: res.data.onevalue.split(","), //字符串重组
                    nameplate: res.data.onevalue.split(",")[0] , //默认选中第一个
                })
                Api.get(app.globalData.mallDomain, { //3）取指定属性商品的价格和库存 获取第一个铭牌的wsid
                    doProductAttributePrice: 1,
                    wpid: e.currentTarget.dataset.wpid,
                    onevalue: res.data.onevalue.split(",")[0],
                }).then(ret => {
                    self.setData({
                        wsid:ret.data.wsid
                    })
                })
            }
        })
        switch (e.currentTarget.dataset.text) {
            case '送自己':
                self.setData({
                    sendType:'送自己'
                })
                break;
            case '送他人':
                self.setData({
                    sendType: '送他人'
                })
                break;
        }
    },
    /**
     * 添加数量
     */
    addVal:function(){
        let self = this;
        self.setData({
            purchaseQuantity: self.data.purchaseQuantity + 1,
        })
    },
    /** 
     * 减少数量
    */
    reductionVal: function () {
        let self = this;
        if (self.data.purchaseQuantity != 1) {
            self.setData({
                purchaseQuantity: self.data.purchaseQuantity - 1,
            })
        }
    },
    /**
     * 点击铭牌列表
     */
    clickNameplateList: function (e) {
        let self = this;
        self.setData({
            nameplate: e.currentTarget.dataset.text,
            activeNameplateList:e.currentTarget.dataset.index,
        })
        Api.get(app.globalData.mallDomain, { //3）取指定属性商品的价格和库存 
            doProductAttributePrice: 1,
            wpid: app.globalData.wpid, //点击送自己 或者送他人按钮的时候 wpid就存入globalData中
            onevalue: e.currentTarget.dataset.text,
        }).then(ret => {
            self.setData({
                wsid: ret.data.wsid
            })
        })
    },
    /**
     * 动画结束时监听页面index
     */
    listeningSwiper: function (e) {
        let self = this;
        self.setData({
            modalImg: self.data.swiperData[e.detail.current].wpCover,
            wpPrice_1: self.data.swiperData[e.detail.current].wpPrice_1,
            wpPrice: self.data.swiperData[e.detail.current].wpPrice,
        })
        console.log(e,'首页商品切换')
        if (e.detail.source == 'touch') {
            if (e.detail.current == self.data.swiperData.length - 3 && self.data.swiperData.length >= 10) {
                self.setData({
                    pageindex: self.data.pageindex + 1,
                })
                //获取商品信息
                if (self.data.showCloseBtn == true) { //显示搜索内容
                    Api.get(app.globalData.mallDomain, {
                        doAllProduct: 1,
                        pagesize: 10,
                        pageindex: self.data.pageindex,
                        txtkey: self.data.search,
                    }).then(res => {
                        if (res.data.list != '') {
                            res.data.list.forEach(ele => {
                                self.setData({
                                    swiperData: self.data.swiperData.concat({
                                        wpID: ele.wpID,
                                        wpName: ele.wpName,
                                        wpViceName: ele.wpViceName,
                                        wpPrice: parseInt(ele.wpPrice),
                                        wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                                        wpOldPrice: parseInt(ele.wpOldPrice),
                                        wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                                        wpCover: ele.wpCover,
                                        wpImgs: ele.wpImgs,
                                        wpPeos: ele.wpPeos,
                                        wpOrigin: ele.wpOrigin,
                                        wpDetail: ele.wpDetail,
                                        SiteDomainName: ele.SiteDomainName,
                                    })
                                })
                            })
                        } else {
                            self.setData({
                                pageindex: self.data.pageindex - 1,
                            })
                        }

                    })
                } else if (self.data.showCloseBtn == false) { //显示所有商品内容
                    Api.get(app.globalData.mallDomain, {
                        doAllProduct: 1,
                        pagesize: 10,
                        pageindex: self.data.pageindex,
                    }).then(res => {
                        if (res.data.list != '') {
                            res.data.list.forEach(ele => {
                                self.setData({
                                    swiperData: self.data.swiperData.concat({
                                        wpID: ele.wpID,
                                        wpName: ele.wpName,
                                        wpViceName: ele.wpViceName,
                                        wpPrice: parseInt(ele.wpPrice),
                                        wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                                        wpOldPrice: parseInt(ele.wpOldPrice),
                                        wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                                        wpCover: ele.wpCover,
                                        wpImgs: ele.wpImgs,
                                        wpPeos: ele.wpPeos,
                                        wpOrigin: ele.wpOrigin,
                                        wpDetail: ele.wpDetail,
                                        SiteDomainName: ele.SiteDomainName,
                                    })
                                })
                            })
                        } else {
                            self.setData({
                                pageindex: self.data.pageindex - 1,
                            })
                        }
                    })
                }

            } else if (e.detail.current == self.data.swiperData.length - 1) { // 解决动画未结束就滑动到下一张 无法加载下一页的BUG
                self.setData({
                    pageindex: self.data.pageindex + 1,
                })
                //获取商品信息
                if (self.data.showCloseBtn == true) {
                    Api.get(app.globalData.mallDomain, {
                        doAllProduct: 1,
                        pagesize: 10,
                        pageindex: self.data.pageindex,
                        txtkey: self.data.search,
                    }).then(res => {
                        if (res.data.list != '') {
                            res.data.list.forEach(ele => {
                                self.setData({
                                    swiperData: self.data.swiperData.concat({
                                        wpID: ele.wpID,
                                        wpName: ele.wpName,
                                        wpViceName: ele.wpViceName,
                                        wpPrice: parseInt(ele.wpPrice),
                                        wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                                        wpOldPrice: parseInt(ele.wpOldPrice),
                                        wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                                        wpCover: ele.wpCover,
                                        wpImgs: ele.wpImgs,
                                        wpPeos: ele.wpPeos,
                                        wpOrigin: ele.wpOrigin,
                                        wpDetail: ele.wpDetail,
                                        SiteDomainName: ele.SiteDomainName,
                                    })
                                })
                            })
                        } else {
                            self.setData({
                                tipText: '没有数据了',
                                showToast: true,
                            })
                            setTimeout(function () {
                                self.setData({
                                    tipText: '',
                                    showToast: false
                                })
                            }, 1000)
                        }
                    })
                } else if (self.data.showCloseBtn == false) {
                    console.log(self.data.pageindex)
                    Api.get(app.globalData.mallDomain, {
                        doAllProduct: 1,
                        pagesize: 10,
                        pageindex: self.data.pageindex,
                    }).then(res => {
                        if (res.data.list != '') {
                            res.data.list.forEach(ele => {
                                self.setData({
                                    swiperData: self.data.swiperData.concat({
                                        wpID: ele.wpID,
                                        wpName: ele.wpName,
                                        wpViceName: ele.wpViceName,
                                        wpPrice: parseInt(ele.wpPrice),
                                        wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                                        wpOldPrice: parseInt(ele.wpOldPrice),
                                        wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                                        wpCover: ele.wpCover,
                                        wpImgs: ele.wpImgs,
                                        wpPeos: ele.wpPeos,
                                        wpOrigin: ele.wpOrigin,
                                        wpDetail: ele.wpDetail,
                                        SiteDomainName: ele.SiteDomainName,
                                    })
                                })
                            })
                        } else {
                            self.setData({
                                tipText: '没有数据了',
                                showToast: true,
                            })
                            setTimeout(function () {
                                self.setData({
                                    tipText: '',
                                    showToast: false
                                })
                            }, 1000)
                        }
                    })
                }
            }
        }
    },


    /**
     * 关闭弹出框
     */
    closeBg: function (e) {
        let self = this;
        self.setData({
            showModal: false,
        })
    },

    /**
     * 关闭搜索内容
     */
    closeSearch: function (e) {
        let self = this;
        //获取商品信息
        Api.get(app.globalData.mallDomain, {
            doAllProduct: 1,
            pagesize: 10,
            pageindex: 1,
        }).then(res => {
            let list = [];
            res.data.list.forEach(ele => {
                list.push({
                    wpID: ele.wpID,
                    wpName: ele.wpName,
                    wpViceName: ele.wpViceName,
                    wpPrice: parseInt(ele.wpPrice),
                    wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                    wpOldPrice: parseInt(ele.wpOldPrice),
                    wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                    wpCover: ele.wpCover,
                    wpImgs: ele.wpImgs,
                    wpPeos: ele.wpPeos,
                    wpOrigin: ele.wpOrigin,
                    wpDetail: ele.wpDetail,
                    SiteDomainName: ele.SiteDomainName,
                })
            })
            self.setData({
                showCloseBtn: false,
                current: 0,
                pageindex: 1,
                swiperData: list
            })
        })
    },
    /**
     * 点击搜索按钮
     */
    clickSearch: function (e) {
        let self = this;
        //获取商品信息
        Api.get(app.globalData.mallDomain, {
            doAllProduct: 1,
            pagesize: 10,
            pageindex: 1,
            txtkey: self.data.search,
        }).then(res => {
            let list = [];
            res.data.list.forEach(ele => {
                list.push({
                    wpID: ele.wpID,
                    wpName: ele.wpName,
                    wpViceName: ele.wpViceName,
                    wpPrice: parseInt(ele.wpPrice),
                    wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                    wpOldPrice: parseInt(ele.wpOldPrice),
                    wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                    wpCover: ele.wpCover,
                    wpImgs: ele.wpImgs,
                    wpPeos: ele.wpPeos,
                    wpOrigin: ele.wpOrigin,
                    wpDetail: ele.wpDetail,
                    SiteDomainName: ele.SiteDomainName,
                })
            })
            self.setData({
                showCloseBtn: true,
                current: 0,
                pageindex:1,
                swiperData: list
            })
        })
    },
    /**
     * 监听搜索值
     */
    searchVal: function (e) {
        let self = this;
        if (e.detail.value == ''){
            self.setData({
                showCloseBtn: false,
            })
        }
        self.setData({
            search: e.detail.value,
        })
        //获取商品信息
        Api.get(app.globalData.mallDomain, {
            doAllProduct: 1,
            pagesize: 10,
            pageindex: 1,
            txtkey: e.detail.value,
        }).then(res => {
            let list = [];
            res.data.list.forEach(ele => {
                list.push({
                    wpID: ele.wpID,
                    wpName: ele.wpName,
                    wpViceName: ele.wpViceName,
                    wpPrice: parseInt(ele.wpPrice),
                    wpPrice_1: ele.wpPrice.substring(ele.wpPrice.length - 2, ele.wpPrice.length),
                    wpOldPrice: parseInt(ele.wpOldPrice),
                    wpOldPrice_1: ele.wpOldPrice.substring(ele.wpOldPrice.length - 2, ele.wpOldPrice.length),
                    wpCover: ele.wpCover,
                    wpImgs: ele.wpImgs,
                    wpPeos: ele.wpPeos,
                    wpOrigin: ele.wpOrigin,
                    wpDetail: ele.wpDetail,
                    SiteDomainName: ele.SiteDomainName,
                })
            })
            if (e.detail.value == '') {
                self.setData({
                    showCloseBtn: false,
                    current: 0,
                    pageindex: 1,
                    swiperData: list
                })
            }else{
                self.setData({
                    showCloseBtn: true,
                    current: 0,
                    pageindex: 1,
                    swiperData: list
                })
            }
        })
    },
    /**
     * 进入详情页面
     */
    showDetail: function (e) {
        let self = this;
        console.log('进入详情页面')
        wx.navigateTo({
            url: '../commodityDetails/commodityDetails?wpID=' + e.currentTarget.dataset.item.wpID
        })
    },
    
   
    
    onUnload: function () {
    },
})
